﻿namespace Dcms.OleDbDAL
{
    using Dcms.BaseLib;
    using Dcms.IDAL;
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.OleDb;

    public class Dcms_CandidateRecordDAL : IDcms_CandidateRecordDAL
    {
        public Dcms_CandidateRecordModel AddModel(Dcms_CandidateRecordModel model)
        {
            string sQLString = "insert into Dcms_CandidateRecord(Cr_pId,Cr_jId,Cr_AddTime,Cr_State) values(@Cr_pId,@Cr_jId,@Cr_AddTime,@Cr_State)";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Cr_pId", OleDbType.Integer), new OleDbParameter("@Cr_jId", OleDbType.Integer), new OleDbParameter("@Cr_AddTime", OleDbType.DBTimeStamp), new OleDbParameter("@Cr_State", OleDbType.VarChar) };
            cmdParms[0].Value = model.Cr_pId;
            cmdParms[1].Value = model.Cr_jId;
            cmdParms[2].Value = model.Cr_AddTime;
            cmdParms[3].Value = model.Cr_State;
            model.Cr_Id = OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public IList<Dcms_CandidateRecordModel> DataSetToList(DataSet ds)
        {
            IList<Dcms_CandidateRecordModel> list = new List<Dcms_CandidateRecordModel>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Dcms_CandidateRecordModel item = new Dcms_CandidateRecordModel();
                if (ds.Tables[0].Rows[i]["Cr_Id"].ToString() != "") item.Cr_Id = int.Parse(ds.Tables[0].Rows[i]["Cr_Id"].ToString());
                if (ds.Tables[0].Rows[i]["Cr_pId"].ToString() != "") item.Cr_pId = int.Parse(ds.Tables[0].Rows[i]["Cr_pId"].ToString());
                if (ds.Tables[0].Rows[i]["Cr_jId"].ToString() != "") item.Cr_jId = int.Parse(ds.Tables[0].Rows[i]["Cr_jId"].ToString());
                if (ds.Tables[0].Rows[i]["Cr_AddTime"].ToString() != "") item.Cr_AddTime = DateTime.Parse(ds.Tables[0].Rows[i]["Cr_AddTime"].ToString());
                item.Cr_State = ds.Tables[0].Rows[i]["Cr_State"].ToString();
                list.Add(item);
            }
            return list;
        }

        public bool DeleteAllCheck(string AllID)
        {
            return OleDbHelper.ExecuteSql("delete from Dcms_CandidateRecord where Cr_Id In (" + AllID + ")", null) > 0;
        }

        public void DeleteModelByID(int ID)
        {
            string sQLString = "delete from Dcms_CandidateRecord where Cr_Id=@Cr_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Cr_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
        }

        public string GetDataCount(string SqlStr)
        {
            return OleDbHelper.GetNoteCount(SqlStr).ToString();
        }

        public DataSet GetDataListByPage(string SqlStr, int CurPage, int PageSize)
        {
            return OleDbHelper.CreateDataSet(SqlStr, (CurPage - 1) * PageSize, PageSize);
        }

        public Dcms_CandidateRecordModel GetModelByID(int ID)
        {
            string sQLString = "select * from Dcms_CandidateRecord where Cr_Id=@Cr_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Cr_Id", OleDbType.Integer) };
            cmdParms[0].Value = ID;
            Dcms_CandidateRecordModel model = new Dcms_CandidateRecordModel();
            DataSet ds = OleDbHelper.CreateDataSet(sQLString, cmdParms);
            IList<Dcms_CandidateRecordModel> list = this.DataSetToList(ds);
            return ((list.Count > 0) ? list[0] : null);
        }

        public DataSet GetModelList()
        {
            string sql = "select * from Dcms_CandidateRecord";
            return OleDbHelper.CreateDataSet(sql);
        }

        public Dcms_CandidateRecordModel Update(Dcms_CandidateRecordModel model)
        {
            string sQLString = "update Dcms_CandidateRecord set Cr_pId=@Cr_pId,Cr_jId=@Cr_jId,Cr_AddTime=@Cr_AddTime,Cr_State=@Cr_State where Cr_Id=@Cr_Id";
            OleDbParameter[] cmdParms = new OleDbParameter[] { new OleDbParameter("@Cr_pId", OleDbType.Integer), new OleDbParameter("@Cr_jId", OleDbType.Integer), new OleDbParameter("@Cr_AddTime", OleDbType.DBTimeStamp), new OleDbParameter("@Cr_State", OleDbType.VarChar), new OleDbParameter("@Cr_Id", OleDbType.Integer) };
            cmdParms[0].Value = model.Cr_pId;
            cmdParms[1].Value = model.Cr_jId;
            cmdParms[2].Value = model.Cr_AddTime;
            cmdParms[3].Value = model.Cr_State;
            cmdParms[4].Value = model.Cr_Id;
            OleDbHelper.ExecuteSql(sQLString, cmdParms);
            return model;
        }

        public bool UpdateState(string AllID, int StateValue)
        {
            return OleDbHelper.ExecuteSql("Update Dcms_CandidateRecord Set Cr_State='" + StateValue.ToString() + "' where Cr_Id In (" + AllID + ")", null) > 0;
        }
    }
}

